package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.CheckDetailsDao;
import cn.gson.springboot.model.dao.afterdao.CucheckDao;
import cn.gson.springboot.model.dao.afterdao.ExpenseDao;
import cn.gson.springboot.model.mapper.aftermapper.ExpenseMapper;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.vo.aftervo.ExpenseVo;
import cn.gson.springboot.model.vo.aftervo.MaintainChartVo;
import cn.gson.springboot.model.vo.aftervo.PieDoubleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ExpenseService {

    @Autowired
    ExpenseDao dao;
    @Autowired
    ExpenseMapper mapper;
    @Autowired
    CucheckDao cucheckDao;
    @Autowired
    CheckDetailsDao checkDetailsDao;

    //分页查询销售费用表
    public List<Expense> selectExpsPage(Integer employeesId,Integer fatherDeptId,String states,String search){
        return mapper.expsPage(employeesId,fatherDeptId,states,search);
    }

    //查询所有部门下员工
    public List<Dept> selectEmp(){
        return mapper.empAll();
    }

    //通过员工id查询出员工姓名
    public String selectEmpName(Integer employeesId){
        return mapper.empByEmpName(employeesId);
    }

    //新增销售费用表
    public void addExps(ExpenseVo exps){
        Expense expn = new Expense();
        if(exps.getEmpId() != 0) {
            expn.setEmpId(exps.getEmpId());     //审批人
        }
        expn.setEmpByEmployeesId(exps.getEmpByEmployeesId());
        expn.setExpsId(exps.getExpsId());
        expn.setExpsDate(exps.getExpsDate());
        expn.setExpsMoney(exps.getExpsMoney());
        expn.setExpsSort(exps.getExpsSort());
        expn.setExpsNote(exps.getExpsNote());
        expn.setExpsPurpose(exps.getExpsPurpose());
        expn.setExpsRemark(exps.getExpsRemark());
        expn.setExpsNumber(exps.getExpsNumber());
        expn.setExpsGenre(exps.getExpsGenre());
        expn.setExpsRate(exps.getExpsRate());
        expn.setExpsDelete(1);
        expn.setCustomerByCustomId(exps.getCustomerByCustomId());
        CucheckType type = new CucheckType();
        type.setCutId(2);
        if(exps.getCucheckTypeByCutId().getCutId() == 0) {
            expn.setCucheckTypeByCutId(type);
        }else{
            expn.setCucheckTypeByCutId(exps.getCucheckTypeByCutId());
        }
        if(exps.getCucheckTypeByCutId().getCutId() != 4){
            expn.setExpsRefus(1);
        }else {
            expn.setExpsRefus(2);
        }
        dao.save(expn);
    }

    //回显销售费用表
    public Expense selectExpsId(Integer expsId){
        return mapper.expsAll(expsId);
    }

    //回显员工id
    public String selectEmpId(Integer expsId){
        return mapper.expsByempId(expsId);
    }

    //逻辑删除
    public void deleteByExpsId(Integer apprId, Integer expsId){
        if(expsId != null){
            Expense exps = dao.findById(expsId).get();
            exps.setExpsDelete(0);
            dao.save(exps);
        }
        if(apprId != null){
            Cucheck cucheck = cucheckDao.findById(apprId).get();
            cucheck.setApprDelete(0);
            cucheckDao.save(cucheck);
        }
    }

    //撤销
    public void revoke(Integer apprId,Integer expsId,Integer empId){
        Timestamp time = new Timestamp(new Date().getTime());
        Expense exps = dao.findById(expsId).get();
        CucheckType type = new CucheckType();
        type.setCutId(6);
        exps.setCucheckTypeByCutId(type);
        dao.save(exps);
        CheckDetails details = new CheckDetails();
        Emp empOne = new Emp();
        empOne.setEmployeesId(empId);
        details.setApprId(apprId);
        details.setApprsDate(time);
        details.setEmpByEmployeesId(empOne);
        details.setCucheckTypeByCutId(type);
        checkDetailsDao.save(details);
        Cucheck check = cucheckDao.findById(apprId).get();
        check.setCucheckTypeByCutId(type);
        check.setEmpByEmployeesId(empOne);
        cucheckDao.save(check);
    }

    //报销
    public void dispatch(Integer expsId){
        Expense exps = dao.findById(expsId).get();
        exps.setExpsRefus(2);
        dao.save(exps);
    }

    //费用类别分布
    public List<MaintainChartVo> selectExpsBySort(Integer employeesId, Integer fatherDeptId){
        return mapper.expsBySort(employeesId, fatherDeptId);
    }

    //全年费用/人员分布（已审核）
    public List<PieDoubleVo> selectExpsByMoney(Integer employeesId, Integer fatherDeptId){
        return mapper.expsByMoney(employeesId, fatherDeptId);
    }

    //月度费用申报统计月份
    public List<String> selectExpsByMonth(Integer employeesId, Integer fatherDeptId){
        return mapper.expsByMonth(employeesId, fatherDeptId);
    }

    //月度费用申报统计数
    public List<Double> selectMonthByMoney(Integer employeesId, Integer fatherDeptId){
        return mapper.monthByMoney(employeesId, fatherDeptId);
    }

    //客户费用（已审核）统计客户
    public List<String> selectExpsByCustom(Integer employeesId, Integer fatherDeptId){
        return mapper.expsByCustom(employeesId, fatherDeptId);
    }

    //客户费用（已审核）统计数
    public List<Double> selectCustomBySum(Integer employeesId, Integer fatherDeptId){
        return mapper.customBySum(employeesId, fatherDeptId);
    }

    //首先验证用户的身份,看用户是不是一级角色
    public List<Map<String, Object>> selectPostLevelEmp(Integer employeesId){
        return mapper.postLevelByEmp(employeesId);
    }

    //如果是普通员工，则查询出同部门拥有一级角色的员工
    public Integer selectOrdinaryByTwo(Integer deptsId){
        return mapper.ordinaryByTwo(deptsId);
    }

    //如果是一级角色，则查询出上级部门的一级角色
    public Integer selectOrdinaryByOne(Integer deptsId){
        return mapper.ordinaryByOne(deptsId);
    }

    //查出拥有最高权限的员工
    public Integer selectOrdinaryByBoss(){
        return mapper.ordinaryByBoss();
    }
}
